package com.luck.emp.th.web;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.luck.emp.entity.EmpTThD;
import com.luck.emp.entity.EmpTThM;
import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.th.logic.IThLogic;
import com.luck.emp.th.qry.ThQry;
import com.luck.emp.wh.logic.IWhLogic;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class ThAction extends BaseActionSupport {
		/**
		 * 逻辑处理类
		 */
		IThLogic thLogic;
		IWhLogic whLogic;
		/**
		 * 查询对象
		 */
		ThQry qry;
		/**
		 * 分页对象
		 **/
		Page page;
		/**
		 * 实体类
		 */
		EmpTThM entity;
		String[] productCodes;//商品数组
		String [] priceReals;//实际采购价
		String[] qtys;//数量数组
		List<EmpTThD> thds;
		List<EmpTWarehouse> whList;
		Long[] ids;
		/**
		 * 列表
		 */
		public String list() throws Exception{
			UserBean userInfo = super.getUserInfo();
			
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (ThQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new ThQry();
			}
			if(qry.getStartDate() == null)
				qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
			if(qry.getEndDate() == null)
				qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
			qry.setUserInfo(userInfo);
			page = thLogic.findThPage(qry, Page.class);
			return "list";
		}
		public String auditList() throws Exception{
			UserBean userInfo = super.getUserInfo();
			
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (ThQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new ThQry();
			}
			if(qry.getStartDate() == null)
				qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
			if(qry.getEndDate() == null)
				qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
			qry.setUserInfo(userInfo);
			page = thLogic.findThPage(qry, Page.class);
			return "auditList";
		}
		/**
		 * 退货审核
		 * @return
		 * @throws Exception
		 */
		public String toAudit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = thLogic.get(EmpTThM.class,entity.getThCode());
			thds = thLogic.findThd(entity.getThCode());
			super.title="商品退货审核";
			if(entity.getStatus().equals("2")){
				super.msgStr = "此退货单已经审核通过，不能再审核";
				return super.GLOBAL_ERROR;
			}
			return "audit";
		}
		public String audit() throws Exception{
			UserBean userBean = super.getUserInfo();
			entity = thLogic.get(EmpTThM.class,entity.getThCode());
			thLogic.saveThAudit(userBean, entity, qry);
			super.redirectUrl = "/emp/th_auditList.action";
			return super.GLOBAL_SUCCESS;
		}
		/**
		 * 新增
		 **/
		public String toAdd() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = new EmpTThM();
			whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
			super.event="ADD";
			super.title="新增商品退货";
			return "edit";
		}
		/**
		 * 修改
		 **/
		public String toEdit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = thLogic.get(EmpTThM.class,entity.getThCode());
			if(!entity.getStatus().equals("1")){
				super.msgStr="此单非新建状态，不可修改";
				return GLOBAL_ERROR;
			}
			whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
			thds = thLogic.findThd(entity.getThCode());
			super.event="EDIT";
			super.title="修改商品退货";
			return "edit";
		}
		/**
		 * 修改动作
		 **/
		public String edit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			String str = "";
			try {
				if("ADD".equals(super.event)){
					//新增操作
					entity.setThCode(null);
					str = thLogic.saveTh(userInfo, entity,productCodes, priceReals, qtys,ids);
					super.addAttribute("qry.pk", entity.getThCode());
				}else if("EDIT".equals(super.event)){
					//修改操作
					EmpTThM db = thLogic.get(EmpTThM.class,entity.getThCode());
					super.copyProperties(db, entity);
					str = thLogic.saveTh(userInfo, db,productCodes, priceReals, qtys,ids);
					super.addAttribute("qryHex", super.qryHex);
				}
			} catch (Exception e) {
				str = e.getMessage();
			}

			if(StringUtils.isNotBlank(str)){
				super.msgStr = str;
				return super.GLOBAL_ERROR;
			}
			super.redirectUrl = "/emp/th_list.action";
			return super.GLOBAL_SUCCESS;
		}
		/**
		 * 删除
		 **/
		public String delete() throws Exception{			
			UserBean userInfo = super.getUserInfo();
			entity = thLogic.get(EmpTThM.class,entity.getThCode());
			String str = "";
			try {
				str = this.thLogic.deleteTh(userInfo, entity);;
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				this.msgStr = str;
				super.renderJsonError(str);
			}else
				super.renderJsonSuccess("删除成功!");
			return null;
		}
		
		/**
		 * 修改
		 **/
		public String view() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = thLogic.get(EmpTThM.class,entity.getThCode());
			thds = thLogic.findThd(entity.getThCode());
			super.title="商品退货详细信息";
			return "view";
		}

		public String print() throws Exception{
			entity = thLogic.get(EmpTThM.class,entity.getThCode());
			thds = thLogic.findThd(entity.getThCode());
			super.title="商品退货详细信息";
			return "print";
		}
		
		public ThQry getQry(){
			return this.qry;
		}
		public void setQry(ThQry qry){
			this.qry = qry;
		}
		
		public IThLogic getThLogic(){
			return this.thLogic;
		}
		public void setThLogic(IThLogic thLogic){
			this.thLogic = thLogic;
		}
		public Page getPage() {
			return this.page;
		}
		public void setPage(Page page) {
			this.page = page;
		}

		public EmpTThM getEntity() {
			return entity;
		}

		public void setEntity(EmpTThM entity) {
			this.entity = entity;
		}

		public String[] getProductCodes() {
			return productCodes;
		}

		public void setProductCodes(String[] productCodes) {
			this.productCodes = productCodes;
		}

		public String[] getPriceReals() {
			return priceReals;
		}

		public void setPriceReals(String[] priceReals) {
			this.priceReals = priceReals;
		}

		public String[] getQtys() {
			return qtys;
		}

		public void setQtys(String[] qtys) {
			this.qtys = qtys;
		}

		public List<EmpTThD> getThds() {
			return thds;
		}

		public void setThds(List<EmpTThD> thds) {
			this.thds = thds;
		}

		public IWhLogic getWhLogic() {
			return whLogic;
		}

		public void setWhLogic(IWhLogic whLogic) {
			this.whLogic = whLogic;
		}

		public List<EmpTWarehouse> getWhList() {
			return whList;
		}

		public void setWhList(List<EmpTWarehouse> whList) {
			this.whList = whList;
		}

		public Long[] getIds() {
			return ids;
		}

		public void setIds(Long[] ids) {
			this.ids = ids;
		}

}